package com.platform.car.app;

import com.platform.car.model.Lock;
import com.platform.car.service.LockService;
import com.platform.car.socket.SocketUtil;
import com.platform.upms.model.json.Tip;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 2017-11-17 09:58:55 韩榕榕 迁移
 * 地锁管理 客户端 Controller
 * 地锁管理 客户端 Controller
 *
 * @author GaoXiang
 * @version 1.0
 */
@Controller
@RequestMapping("/car/app/lock")
public class AppLockController {

    /**
     * 修改地锁（地锁的开关为关闭）
     * 2017-05-31 15:46:55 韩榕榕
     * 2017/06/03 高祥
     * @param lockId 地锁的Id
     * @return 成功状态
     */
    @RequestMapping("/updateClose")
    @ResponseBody
    public Tip updateClose(String lockId) {
        Lock lock = lockService.get(lockId);
        if (lock != null) {
            try {
                lock.setLockIo("0");
                lockService.update(lock);
//                SocketUtil.socketTaskMap.get(lock.getNumber()).closeLock();
                return new Tip();
            } catch (Exception e) {
                e.printStackTrace();
                return new Tip(1, "数据修改异常！");
            }
        } else {
            return new Tip(2, "地锁不存在");
        }
    }

    @RequestMapping("/start")
    @ResponseBody
    public Tip start() {
        try {
            SocketUtil.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Tip();
    }

    /**
     * 修改地锁（地锁开关为开）
     * 2017-5-31 15:59:57 韩榕榕
     * 2017/06/03 高祥
     * @param lockId
     * @return
     */
    @RequestMapping("/updateOpen")
    @ResponseBody
    public Tip updateOpen(String lockId) {
        Lock lock = lockService.get(lockId);
        if (lock != null) {
            try {
                lock.setLockIo("1");
                lockService.update(lock);
                SocketUtil.socketTaskMap.get(lock.getNumber()).openLock();
                return new Tip();
            } catch (Exception e) {
                e.printStackTrace();
                return new Tip(1, "数据修改异常");
            }
        } else {
            return new Tip(2, "地锁不存在");
        }
    }

    /**
     * 修改地锁的状态
     * 2017-5-31 16:11:06 韩榕榕
     *
     * @param lockId 地锁的ID
     * @param status 地锁的状态  0：空闲,1：已预约,2：使用中,3：电压不足
     * @return
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Tip updateStatus(String lockId, String status) {
        Lock lock = lockService.get(lockId);
        if (lock != null) {
            try {
                lock.setStatus(status);
                lockService.update(lock);
                SocketUtil.socketTaskMap.get(lock.getNumber()).yuyue();
                return new Tip();
            } catch (Exception e) {
                e.printStackTrace();
                return new Tip(1, "数据修改异常");
            }
        } else {
            return new Tip(2, "地锁不存在");
        }
    }


    //---------------------------- property -------------------------------

    @Resource
    private LockService lockService;

}
